package com.edu.zju.scs.panacea.controller.resource;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.resource.Site;
import com.edu.zju.scs.panacea.entity.resource.Template;
import com.edu.zju.scs.panacea.service.resource.TemplateService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-19 18:48
 */


@Controller
@EnableAutoConfiguration
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @ResponseBody
    @ApiOperation(value="根据id查询某个模板", notes="根据id查询某个模板")
    @RequestMapping(value = "/template/{id}", method = RequestMethod.GET)
    public BaseResult getTemplate(@PathVariable Long id) {
        return templateService.getTemplate(id);
    }


    @ResponseBody
    @ApiOperation(value="查询某个站点下的所有模板", notes="查询某个站点下的所有模板")
    @RequestMapping(value = "/template/site", method = RequestMethod.GET)
    public BaseResult getTemplateBySiteId(HttpSession session) {
        Site site = (Site) session.getAttribute("site");
        return templateService.getTemplateBySiteId(site.getId());
    }

    @ResponseBody
    @ApiOperation(value="根据ID删除模板", notes="根据ID删除模板")
    @RequestMapping(value = "/template/deletion/{id}", method = RequestMethod.GET)
    public BaseResult deleteTemplate(@PathVariable Long id) {
        return templateService.deleteTemplate(id);
    }

    @ResponseBody
    @ApiOperation(value="新增模板", notes="新增模板")
    @RequestMapping(value = "/template/new", method = RequestMethod.POST)
    public BaseResult addTemplate(
            @ApiParam("类型") @RequestParam(value = "type") Integer type,
            @ApiParam("名称") @RequestParam(value = "name") String name,
            @ApiParam("允许大小") @RequestParam(value = "maxSize") Integer maxSize,
            @ApiParam("高度") @RequestParam(value = "height") Integer height,
            @ApiParam("宽度") @RequestParam(value = "width") Integer width,
            @ApiParam("文件格式") @RequestParam(value = "format") List<Integer> format,
            HttpSession session) {
        Site site = (Site) session.getAttribute("site");

        Template template = new Template();
        template.setSiteId(site.getId());
        template.setType(type);
        template.setName(name);
        template.setMaxSize(maxSize);
        template.setHeight(height);
        template.setWidth(width);
        template.setFormat(format);
        return templateService.addTemplate(template);
    }



    @ResponseBody
    @ApiOperation(value="修改模板信息", notes="修改模板信息")
    @RequestMapping(value = "/template/edit", method = RequestMethod.POST)
    public BaseResult editTemplate(
            @ApiParam("ID") @RequestParam(value = "id") Long id,
            @ApiParam("类型") @RequestParam(value = "type") Integer type,
            @ApiParam("名称") @RequestParam(value = "name") String name,
            @ApiParam("允许大小") @RequestParam(value = "maxSize") Integer maxSize,
            @ApiParam("高度") @RequestParam(value = "height") Integer height,
            @ApiParam("宽度") @RequestParam(value = "width") Integer width,
            @ApiParam("文件格式") @RequestParam(value = "format") List<Integer> format,
            HttpSession session) {
        Site site = (Site) session.getAttribute("site");

        Template template = new Template();
        template.setId(id);
        template.setSiteId(site.getId());
        template.setType(type);
        template.setName(name);
        template.setMaxSize(maxSize);
        template.setHeight(height);
        template.setWidth(width);
        template.setFormat(format);
        return templateService.editTemplate(template);
    }


    @ResponseBody
    @ApiOperation(value="根据资源位列表和类型查找模板信息", notes="根据资源位列表和类型查找模板信息")
    @RequestMapping(value = "/template/locs", method = RequestMethod.POST)
    public BaseResult getTemplateByLocs(
            @ApiParam("资源位列表") @RequestParam(value = "locIds") List<Long> locIds,
            @ApiParam("模板类型") @RequestParam(value = "type") Integer type) {
        return templateService.getTemplateByLocs(locIds, type);
    }
}
